/**
 * Created by Administrator on 2017/4/24.
 */
$(function(){

    function getUrl(){
        var APPID = "wx4984a29d10ea78e7";
        var REDIRECT_URI ='http%3A%2F%2Fmimi.youyuzone.com%2Fview%2Findextest.html';
        var SCOPE="snsapi_userinfo";
        var local_url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+APPID+'&redirect_uri='+REDIRECT_URI+'&response_type=code&scope='+SCOPE +'&state=STATE#wechat_redirect ';
        return local_url;
    }
    function getParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");
        var r = window.location.search.substr(1).match(reg);
        if (r!=null) return (r[2]); return null;
    }
    var code = getParam('code');
    if(code){
        var tokenParam = {'code':code,'serviceAccount':true,'channelId':'youyu_zhubo_tiantianreliao_1','inviteCode':12368};
        var time = new Date().getTime();
        var sign = $.md5('ts='+ time +'&mn=3&chid=0&version=1.0&param='+JSON.stringify(tokenParam));
        var reqTokenObj = {
            time:time,
            url: 'http://120.77.152.88:8686/oneTalkOnline//login/auto_register_wx',
            param:tokenParam,
            sign:sign,
            okCallback:tokenOkCallback,
            errorCallback:errorCallback
        }
        function errorCallback(){
            alert("请求失败");
        }

        function userInfoOkCallback(userInfoOkCallbackData){
            if(userInfoOkCallbackData.result == 200 && userInfoOkCallbackData.data.token){
                localStorage.setItem('youyuLoginToken',userInfoOkCallbackData.data.token);
                localStorage.setItem('youyuUserData',JSON.stringify(userInfoOkCallbackData.data.userData));
            }
        }

        function tokenOkCallback(tokenOkCallbackData){
            console.log(tokenOkCallbackData);
            var access_token = tokenOkCallbackData.data.access_token;
            var openid = tokenOkCallbackData.data.openid;
             localStorage.setItem('youyuOpenid',openid );
            var infoUrl =WECHAT_USER;

            var curtime = new Date().getTime();
            var param = {'access_token':access_token,'openid':openid};

            var reqUserInfoObj = {
                time:curtime,
                url:infoUrl,
                param:param,
                sign:$.md5('ts='+ curtime +'&mn=3&chid=0&version=1.0&param='+JSON.stringify(param)),
                okCallback:userInfoOkCallback,
                errorCallback:errorCallback
            }

            YY.post(reqUserInfoObj);
        }

        YY.post(reqTokenObj);
    }
   /* var youyuLoginToken = localStorage.getItem('youyuLoginToken');
    if(!youyuLoginToken) {
        /!* userGold();*!/

        /!*  $('.main').delegate('.pic','click',function() {
         if (golds <= 0) {
         $('#recharge').show();
         } else {
         $('#downLoad').show();
         }
         })*!/
        var code_url = getUrl();
        window.location.href = code_url;
    } */
    /*查询用户金币*/
    /*var golds;
    function userGold() {
        var userData = localStorage.getItem('youyuUserData');
        var token = localStorage.getItem('youyuLoginToken');
        var packUrl = USER_ACCOUNT;
        var curTime = new Date().getTime();
        var uid = $.parseJSON(userData).uid;
        var packParam = {'uid': uid};
        var gold = {
            time: curTime,
            token: token,
            url: packUrl,
            param: packParam,
            sign: $.md5('ts=' + curTime + '&mn=3&chid=0&version=1.0&token=' + token + '&param=' + JSON.stringify(packParam)),
            okCallback: function (data) {
                golds = data.data;
            },
            errorCallback: function () {
            }
        }
        YY.post(gold);
    }
        var youyuLoginToken = localStorage.getItem('youyuLoginToken');
        if(youyuLoginToken){
            userGold();

                $('.main').delegate('.pic','click',function() {
                    if (golds <= 0) {
                        $('#recharge').show();
                    } else {
                        $('#downLoad').show();
                    }
                })

        }else {
            var code_url = getUrl();
            window.location.href=code_url;
        }
    //});
    $('.main').delegate('.information','click',function(){
        var aid;
        aid = $(this).data('type');
        window.location.href = "anchor.html"+"?aid="+aid;
    })*/

})

